package com.nari.ysphd.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Process;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.jeremyfeinstein.conference.CustomDialog;
import com.jeremyfeinstein.utils.AppConstants;
import com.jeremyfeinstein.utils.ConfigUtil;
import com.jeremyfeinstein.utils.SPUtil;
import com.jeremyfeinstein.utils.ToastUtil;
import com.nari.ysphd.MyApp;
import com.nari.ysphd.R;
import com.nari.ysphd.util.EmulatorUtil;
import com.nari.ysphd.util.RootUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhatong on 2019/11/11.
 */

public class IPConfigActivity extends BaseActivity {

    private static final String TAG = IPConfigActivity.class.getSimpleName();
    private TextView tv_ipSetting;
    private TextView btn_startWithMIP;

    /**
     * UAP的IP和端口
     */
    private TextView tv_uap_ip, tv_uap_port;

    /**
     * 会商的服务的IP和端口
     */
    private TextView tv_hs_ip, tv_hs_port;

    /**
     * 平台服务IP和端口
     */
    private TextView platform_ip, platform_port;


    /**
     * 取消和确认
     */
    private TextView tv_cancel, tv_confirm;

    /**
     * 设置IP dialog
     */
    private CustomDialog settingIpdialog;

    /**
     * 分别率选择下拉框
     */
    private Spinner spinner_resolution;

    private String uapIP;
    private String uapPort;
    private String platformIP;
    private String platformPort;
    private String ychsIP;
    private String ychsPort;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ipconfig);

        if (RootUtil.isDeviceRooted() || EmulatorUtil.isEmulator(MyApp.getApp())) {
            ToastUtil.showShort(MyApp.getApp(), "当前设备存在安全隐患");
            Process.killProcess(Process.myPid());
            return;
        }

        tv_ipSetting = (TextView) findViewById(R.id.setting);
        btn_startWithMIP = (TextView) findViewById(R.id.start_with_mip);
        initSettingIpView();

        tv_ipSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showIpSettingDialog();
            }
        });

        btn_startWithMIP.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(IPConfigActivity.this, StartActivity.class);
                startActivity(intent);
            }
        });
        initResolution(this);

    }


    private void showIpSettingDialog() {
        uapIP = SPUtil.get(AppConstants.UAP_IP, "").toString();
        uapPort = SPUtil.get(AppConstants.UAP_PORT, "").toString();
        ychsIP = SPUtil.get(AppConstants.YCHS_IP, "").toString();
        ychsPort = SPUtil.get(AppConstants.YCHS_PORT, "").toString();
        platformIP = SPUtil.get(AppConstants.Platform_IP, "").toString();
        platformPort = SPUtil.get(AppConstants.Platform_PORT, "").toString();

        //initParams();

        if (!TextUtils.isEmpty(platformIP)) {
            platform_ip.setText(platformIP);
        }
        if (!TextUtils.isEmpty(platformPort)) {
            platform_port.setText(platformPort);
        }
        if (!TextUtils.isEmpty(uapIP)) {
            tv_uap_ip.setText(uapIP);
        }
        if (!TextUtils.isEmpty(uapPort)) {
            tv_uap_port.setText(uapPort);
        }
        if (!TextUtils.isEmpty(ychsIP)) {
            tv_hs_ip.setText(ychsIP);
        }
        if (!TextUtils.isEmpty(ychsPort)) {
            tv_hs_port.setText(ychsPort);
        }
        if (settingIpdialog != null && !settingIpdialog.isShowing()) {
            settingIpdialog.show();
        }
    }

    private void initParams() {
        uapIP = "172.28.1.151";
        uapPort = "7003";
        ychsIP = "172.16.219.228";
        ychsPort = "14100";
        platformIP = "172.16.219.211";
        platformPort = "13006";
    }

    private void initSettingIpView() {

        settingIpdialog = new CustomDialog()
                .setView(R.layout.login_ip_setting_dialog)
                .setCanceledOnTouchOutside(false)
                .setCancelable(false)
                .builder(IPConfigActivity.this)
                .create()
                .viewManager(new CustomDialog.CallBack() {
                    @Override
                    public void handleView(final Dialog dialog, View dialogView) {
                        tv_uap_ip = (TextView) dialogView.findViewById(R.id.uap_server_ip);
                        tv_uap_port = (TextView) dialogView.findViewById(R.id.uap_server_port);
                        platform_ip = (TextView) dialogView.findViewById(R.id.platform_ip);
                        platform_port = (TextView) dialogView.findViewById(R.id.platform_port);
                        tv_cancel = (TextView) dialogView.findViewById(R.id.cancel);
                        tv_confirm = (TextView) dialogView.findViewById(R.id.confirm);


                        tv_hs_ip = (TextView) dialogView.findViewById(R.id.ychs_server_ip);
                        tv_hs_port = (TextView) dialogView.findViewById(R.id.ychs_server_port);

                        spinner_resolution = (Spinner) dialogView.findViewById(R.id.spinner_resolution);


                        tv_cancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (dialog != null && dialog.isShowing()) {
                                    dialog.dismiss();
                                }
                            }
                        });

                        tv_confirm.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                uapIP = tv_uap_ip.getText().toString().trim();
                                uapPort = tv_uap_port.getText().toString().trim();
                                platformIP = platform_ip.getText().toString().trim();
                                platformPort = platform_port.getText().toString().trim();
                                ychsIP = tv_hs_ip.getText().toString().trim();
                                ychsPort = tv_hs_port.getText().toString().trim();
                                if (TextUtils.isEmpty(platformIP)) {
                                    ToastUtil.showToast("平台服务IP不能为空！");
                                    return;
                                }

                                if (TextUtils.isEmpty(platformPort)) {
                                    ToastUtil.showToast("平台服务端口不能为空！");
                                    return;
                                }
                                if (TextUtils.isEmpty(uapIP)) {
                                    ToastUtil.showToast("UAP服务不能为空！");
                                    return;
                                }
                                if (TextUtils.isEmpty(uapPort)) {
                                    ToastUtil.showToast("UAP端口不能为空！");
                                    return;
                                }

                                if (TextUtils.isEmpty(ychsIP)) {
                                    ToastUtil.showToast("远程会商IP不能为空！");
                                    return;
                                }

                                if (TextUtils.isEmpty(ychsPort)) {
                                    ToastUtil.showToast("远程会商端口不能为空！");
                                    return;
                                }

                                SPUtil.put(AppConstants.Platform_IP, platformIP);
                                SPUtil.put(AppConstants.Platform_PORT, platformPort);
                                SPUtil.put(AppConstants.UAP_IP, uapIP);
                                SPUtil.put(AppConstants.UAP_PORT, uapPort);
                                SPUtil.put(AppConstants.YCHS_IP, ychsIP);
                                SPUtil.put(AppConstants.YCHS_PORT, ychsPort);
                                String UAP_URL = AppConstants.REQUEST_MODE + uapIP + ":" + uapPort;
                                ConfigUtil.getInstance().setRemoteUrl(ychsIP);
                                ConfigUtil.getInstance().setRemotePort(ychsPort);
                                String pc_web = AppConstants.PC_WEB_REQUEST_MODE + ychsIP + ":" + ychsPort;
                                SPUtil.put(AppConstants.PC_WEB, pc_web);

                                SPUtil.put(AppConstants.UAP_URL, UAP_URL);
                                if (dialog != null && dialog.isShowing()) {
                                    dialog.dismiss();
                                }
                            }
                        });
                    }
                });

    }

    private void initResolution(final Activity context) {
        List<String> list = new ArrayList<String>();
        list.add("高清(720)");  //1280x720
        list.add("标清(640)");  //640x480
        list.add("流畅(320)");  //320x240

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
                android.R.layout.simple_spinner_item, list);
        adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
        spinner_resolution.setAdapter(adapter);
        SpinnerAdapter apsAdapter = spinner_resolution.getAdapter(); // 得到SpinnerAdapter对象
        int k = apsAdapter.getCount();
        for (int i = 0; i < k; i++) {
            if (ConfigUtil.getInstance().getResolution()
                    .equals(apsAdapter.getItem(i).toString())) {
                spinner_resolution.setSelection(i, true);// 默认选中项
                break;
            }
        }
        spinner_resolution.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            // parent： 为控件Spinner com.aspsine.swipetoloadlayout.view：显示文字的TextView position：下拉选项的位置从0开始
            @SuppressWarnings("unchecked")
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                ((TextView) parent.getChildAt(0)).setTextColor(Color.parseColor("#565B62"));
                // 获取Spinner控件的适配器
                ArrayAdapter<String> adapter = (ArrayAdapter<String>) parent
                        .getAdapter();
                ConfigUtil.getInstance().setResolution(
                        adapter.getItem(position));
                spinner_resolution.setSelection(position);
            }

            // 没有选中时的处理
            public void onNothingSelected(AdapterView<?> parent) {
                Toast.makeText(context, "请选分辨率", Toast.LENGTH_SHORT).show();
            }
        });
    }

}
